#!/bin/bash
source /etc/profile
if [ $# -eq 1 ]; then
	targetHour=$1
else
	targetHour=`date -d "-1 hours" +"%Y%m%d%H"`
fi
path=$(cd $(dirname $0);pwd)
parentPath=$(dirname ${path})
cd ${parentPath}
targetDate=${targetHour:0:8}
targetAnalysisHour=${targetHour:8:2}
if [ ! -d "${path}/${targetDate}" ];then
	mkdir -p ${path}/${targetDate}
fi
day=$(date -d"${targetDate}" "+%Y-%m-%d")
# 小时计算
hive -e "set mapred.job.name=[NAS][overview][hour][${targetDate}];use formatlog;insert overwrite local directory '${path}/${targetDate}/${targetHour}_overview_hour_result' select a.cid,a.sid,${targetDate},sum(a.pv),count(distinct a.nasid),count(distinct a.ip),sum(a.visit_time)/sum(a.pv),${targetAnalysisHour} from (select cid,sid,session,max(nasid) as nasid,max(ip) as ip,((max(time)-min(time))/1000+60) as visit_time,count(1) as pv from nas_tracking_format_hour where day=${targetDate} and hour=${targetAnalysisHour} and length(cid)>0 and length(sid)>0 group by cid,sid,session) a group by a.cid,a.sid"
wait
#合并小时概览数据
cat ${path}/${targetDate}/${targetHour}_overview_hour_result/* > ${path}/${targetDate}/${targetHour}_overview_hour
wait
# load data to mysql
source ${parentPath}/configs/mysql_config

#nas_visitor_overview_hour
python ${path}/load_visitor_overview_hour_to_mysql.py ${path}/${targetDate}/${targetHour}_overview_hour ${host} ${port} ${user} ${password} ${db} ${day} ${targetAnalysisHour}